<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 6] 6.10 The return Statement</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:14:41 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 6<br>Statements and Control Structures</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-10">6.10 The return Statement</A></h2>

<P CLASS=para>
<A NAME="CH06.RETURN"></A>A <tt CLASS=literal>return</tt> statement returns
control of the current method or constructor to the caller:


<p>
<img align=middle src="./figs/jlr0619.gif" alt="[Graphic: Figure from the text]" width=424 height=27 border=0>

<P CLASS=para>
If a <tt CLASS=literal>return</tt> statement
does not contain an expression, the statement must be in a method
declared with the <tt CLASS=literal>void</tt> return type or in a constructor.
Otherwise, the compiler issues an error message. When a
<tt CLASS=literal>return</tt>
statement does not contain an expression, the statement simply attempts
to transfer control back to the method or constructor that invoked
the current method or constructor.

<P CLASS=para>
If a <tt CLASS=literal>return</tt>
statement contains an expression, it must be in a method that returns
a value or the compiler issues an error message. The type of the
expression must be assignment-compatible with the declared return
type of the method. The <tt CLASS=literal>return</tt> statement attempts
to transfer control back to the method or constructor that invoked
the current method. The value produced by the expression is the
return value of the current method.

<P CLASS=para>
Here's an example of a <tt CLASS=literal>return</tt> statement:

<DIV CLASS=screen>
<P>
<PRE>
int doubleIt (int k) {
    return k*2;
}
</PRE>
</DIV>

<P CLASS=para>
If a <tt CLASS=literal>return</tt> statement occurs inside a <tt CLASS=literal>try</tt>
statement, control may not immediately transfer to the invoking
method or constructor. If a <tt CLASS=literal>try</tt> statement has
a <tt CLASS=literal>finally</tt> clause, the <tt CLASS=literal>finally</tt>
block is executed before control leaves the <tt CLASS=literal>try</tt>
statement for any reason. This means that if a <tt CLASS=literal>return</tt>
statement occurs inside a <tt CLASS=literal>try</tt> statement (but
not in its <tt CLASS=literal>finally</tt> block), the <tt CLASS=literal>finally</tt>
block is executed first, before the control transfer can take place.

<P CLASS=para>
If the <tt CLASS=literal>finally</tt> block contains a
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>,
or <tt CLASS=literal>throw</tt> statement, the pending control transfer
for the previously executed <tt CLASS=literal>return</tt> statement
is forgotten. Instead, control is transferred to the target of the
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>, or <tt CLASS=literal>throw</tt> statement
in the <tt CLASS=literal>finally</tt> block.

<P CLASS=para>
If the <tt CLASS=literal>finally</tt> block does not contain a
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>,
or <tt CLASS=literal>throw</tt> statement, the pending control transfer
happens after the <tt CLASS=literal>finally</tt> block is done executing,
unless there is another enclosing <tt CLASS=literal>try</tt> statement.
If there is such a <tt CLASS=literal>try</tt> statement and it has a
<tt CLASS=literal>finally</tt> clause, that <tt CLASS=literal>finally</tt> block
is also executed before the control transfer can take place. Execution
proceeds in this manner until control is transferred to the invoking
method or constructor.

<P CLASS=para>
<b>References</b>
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.3">Constructors</A>;
<I CLASS=emphasis>Expression</I> 4;
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.1">Identifiers</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.2">Methods</A>;
<A HREF="ch06_08.htm#JLR2-CH-6-SECT-8">The break Statement</A>;
<A HREF="ch06_09.htm#JLR2-CH-6-SECT-9">The continue Statement</A>;
<A HREF="ch06_11.htm#JLR2-CH-6-SECT-11">The throw Statement</A>;
<A HREF="ch06_12.htm#JLR2-CH-6-SECT-12">The try Statement</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_09.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_11.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>The continue Statement</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>The throw Statement</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
